<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="78px">
      <!--      <el-form-item label="行政村" prop="admVillage">
              <el-select v-model="queryParams.admVillage" placeholder="请选择行政村" clearable>
                <el-option
                  v-for="dict in base_administration_village"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="自然村" prop="natVillage">
              <el-input
                v-model="queryParams.natVillage"
                placeholder="请输入自然村"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <el-form-item label="户编号" prop="fid">
        <el-input
            v-model="queryParams.fid"
            placeholder="请输入户编号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户主姓名" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入户主姓名"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号码" prop="id_card">
        <el-input

            placeholder="请输入证件号码"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度" prop="year">
        <el-input

            placeholder="请输入年度"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="户联系电话" prop="phone">
              <el-input
                v-model="queryParams.phone"
                placeholder="请输入户联系电话"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="人口数" prop="num">
              <el-input
                v-model="queryParams.num"
                placeholder="请输入人口数"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="耕种面积" prop="area">
              <el-input
                v-model="queryParams.area"
                placeholder="请输入耕种面积"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="与户主关系" prop="familyType">
              <el-select v-model="queryParams.familyType" placeholder="请选择与户主关系" clearable>
                <el-option
                  v-for="dict in base_family_relationship"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="户状态(0-有人 1-绝户)" prop="state">
              <el-input
                v-model="queryParams.state"
                placeholder="请输入户状态(0-有人 1-绝户)"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['NH:base_family_info:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['NH:base_family_info:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['NH:base_family_info:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['NH:base_family_info:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="base_family_infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="户编号" align="center" prop="fid"/>
      <el-table-column label="行政村" align="center" prop="admVillage">
        <template #default="scope">
          <dict-tag :options="base_administration_village" :value="scope.row.admVillage"/>
        </template>
      </el-table-column>
      <el-table-column label="自然村" align="center" prop="natVillage">
        <template #default="scope">
          <dict-tag :options="base_nature_village" :value="scope.row.natVillage"/>
        </template>
      </el-table-column>
      <el-table-column label="户主姓名" align="center" prop="name"/>
      <el-table-column label="户联系电话" align="center" prop="phone"/>
      <el-table-column label="人口数" align="center" prop="num"/>
      <el-table-column label="耕种面积" align="center" prop="area"/>
      <el-table-column label="户类别" align="center" prop="familyType">
        <template #default="scope">
          <dict-tag :options="base_family_type" :value="scope.row.familyType"/>
        </template>
      </el-table-column>
      <el-table-column label="户状态" align="center" prop="state"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="修改" placement="top" v-if="scope.row.fid !== 1">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['NH:base_family_info:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" v-if="scope.row.fid !== 1">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['NH:base_family_info:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改农户收入信息管理对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="farmerRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="行政村" prop="admVillage">
          <el-select v-model="form.admVillage" placeholder="请选择行政村">
            <el-option
                v-for="dict in base_administration_village"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自然村" prop="natVillage">
          <el-select v-model="form.natVillage" placeholder="请输入自然村">
            <el-option
                v-for="dict in base_nature_village"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="户主姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入户主姓名"/>
        </el-form-item>
        <el-form-item label="户联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入户联系电话"/>
        </el-form-item>
        <el-form-item label="人口数" prop="num">
          <el-input v-model="form.num" placeholder="请输入人口数"/>
        </el-form-item>
        <el-form-item label="耕种面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入耕种面积"/>
        </el-form-item>
        <el-form-item label="户类别" prop="familyType">
          <el-select v-model="form.familyType" placeholder="请选择户类别" clearable>
            <el-option
                v-for="dict in base_family_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="户状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入户状态(0-有人 1-绝户)"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  listBase_family_info,
  getBase_family_info,
  delBase_family_info,
  addBase_family_info,
  updateBase_family_info
} from "@/api/NH/base_family_info";

const router = useRouter();
const {proxy} = getCurrentInstance();
const {
  base_administration_village,
  base_family_type,
  base_nature_village,
} = proxy.useDict("base_administration_village", "base_family_type", "base_nature_village");

// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 农户收入信息管理表格数据***
const base_family_infoList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    fid: null,
    admVillage: null,
    natVillage: null,
    name: null,
    phone: null,
    num: null,
    area: null,
    familyType: null,
    state: null
  },
  rules: {
    natVillage: [
      {required: true, message: "自然村不能为空", trigger: "blur"}
    ],
    name: [
      {required: true, message: "户主姓名不能为空", trigger: "blur"}
    ],
    phone: [
      {required: true, message: "户联系电话不能为空", trigger: "blur"}
    ],
    num: [
      {required: true, message: "人口数不能为空", trigger: "blur"}
    ],
    familyType: [
      {required: true, message: "与户主关系不能为空", trigger: "change"}
    ],
    state: [
      {required: true, message: "户状态不能为空", trigger: "blur"}
    ]
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询农户收入信息管理列表 */
function getList() {
   loading.value = true;
   listBase_family_info(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    base_family_infoList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
};

/** 重置操作表单 */
function reset() {
  form.value = {
    fid: null,
    admVillage: null,
    natVillage: null,
    name: null,
    phone: null,
    num: null,
    area: null,
    familyType: null,
    state: null
  };
  proxy.resetForm("farmerRef");
};

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryForm");
  handleQuery();
};

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.fid);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
function handleAdd() {
  reset();
   getBase_family_info().then(response => {
    open.value = true;
    title.value = "添加农户收入信息";
  });
};

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const fid = row.fid || ids.value;
   getBase_family_info(fid).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改农户收入信息";
  });
};

/** 提交按钮 */
function submitForm() {
  proxy.$refs["farmerRef"].validate(valid => {
    if (valid) {
      if (form.value.fid != undefined) {
        updateBase_family_info(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBase_family_info(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};

/** 删除按钮操作 */
function handleDelete(row) {
  const fids = row.fid || ids.value;
  proxy.$modal.confirm('是否确认删除农户编号为"' + fids + '"的收入信息数据项？').then(function () {
    return delBase_family_info(fids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
};

/** 导出按钮操作 */
function handleExport() {
  proxy.download("NH/base_family_info/export", {
    ...queryParams.value,
  }, `base_family_info_${new Date().getTime()}.xlsx`);
};
getList();
</script>
